﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;

namespace XsltDbClient
{
    public partial class Settings : Form
    {
        private static XmlDocument doc;
        private static string configPath;
        static Settings()
        {
            doc = new XmlDocument();
            configPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
            doc.Load(configPath);
        }

        public Settings()
        {
            InitializeComponent();
            txtDomain.Text = Portal;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Portal = txtDomain.Text;
        }

        public static string Portal
        {
            get
            {
                return doc.SelectSingleNode("/configuration/appSettings/add[@key='portal']/@value").Value;
            }
            set
            {
                doc.SelectSingleNode("/configuration/appSettings/add[@key='portal']/@value").Value = value;
                doc.Save(configPath);
            }
        }

        /// <summary>
        /// 01.01.00.09: store login information in config file
        /// </summary>
        public static string Login
        {
            get
            {
                try
                {
                    return doc.SelectSingleNode("/configuration/appSettings/add[@key='login']/@value").Value;
                }
                catch(Exception)
                {
                    return String.Empty;
                }
            }
            set
            {
                try
                {
                    doc.SelectSingleNode("/configuration/appSettings/add[@key='login']/@value").Value = value;
                }
                catch (Exception)
                {
                    //must add new key
                    XmlElement newkey =  doc.CreateElement("add");
                    newkey.SetAttribute("key", "login");
                    newkey.SetAttribute("value", value);
                    doc.SelectSingleNode("/configuration/appSettings").AppendChild(newkey);
                }

                doc.Save(configPath);
            }
        }
    }
}
